import json
import re


# 原始背景车文件路径
input_file_path = r'D:\hsy\04-西沙屯DEMO\02-project\cd_playoff_online_ruanjianyuan\Log\scene11-1733998070.json'
# 删除主车的背景车文件路径
output_file_path = r'D:\hsy\04-西沙屯DEMO\02-project\cd_playoff_online_ruanjianyuan\Log\scene11-result.json'
# 要删除的背景车ID
delete_target_id = 100001

def clean_json_string(line):
    """尝试修复JSON字符串中的常见格式问题"""
    # 将单引号替换为双引号，但保留字符串内部的单引号不变
    cleaned_line = re.sub(r"(?<!\\)'(?!')", r'"', line)
    return cleaned_line

def filter_vehicle_data(line):
    """从一行JSON数据中移除id为100001的对象"""
    try:
        # 尝试修复JSON格式问题
        cleaned_line = clean_json_string(line)
        data = json.loads(cleaned_line)
        if 'value' in data:
            data['value'] = [item for item in data.get('value', []) if item.get('id') != delete_target_id]
        return json.dumps(data, ensure_ascii=False) + '\n'
    except json.JSONDecodeError as e:
        print(f"JSON解码错误: {e}. 跳过此行: {line.strip()}")
        return None

# 读取原始文件并过滤数据
with open(input_file_path, 'r', encoding='utf-8') as infile, \
     open(output_file_path, 'w', encoding='utf-8') as outfile:
    for line in infile:
        filtered_line = filter_vehicle_data(line.strip())
        if filtered_line is not None:
            outfile.write(filtered_line)

print("处理完成，已生成新的JSON文件。")
